﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<script type="text/javascript">

//load scriptting
$(document).ready(function () {
	// 플랜 리스트
	planGrpListAjax();
	
    $("#planNm").bind("keydown", function(e) {
        if (e.keyCode == 13) { // enter key
        	if($("#planGrpNo").val() == ""){
        		planGrpRegistAjax();  
        	}else{
        		planRegistAjax();   
        	}
        	  
            return false;   
        } 

    });   
	
});    

function planDeleteAjax(planNo){  
	$.ajax({  
		type:"post"   
		,url:"/plan/planDeleteAjax.do"
		,data:{"planNo":planNo}
		,dataType:"json"
		,success:function(data){
			if(data == 1){  
				planListAjax($("#planGrpNo").val());  
			}
		}
	});
}   
   
function planListAjax(planGrpNo) {
	$("#planGrpNo").val(planGrpNo);   
	$('#popupMenu').popup('close');   
	$("#registBtn").attr("href", "javascript:planRegistAjax();");
	$.ajax({
		type:"post"
		,url:"/plan/planListAjax.do"
		,data:{"planGrpNo":planGrpNo}
		,dataType:"json"     
		,success:function(data){   
			var planMainDiv = "";  
			if(data.length > 0){     
				for(var i=0; i<data.length;i++){      
					planMainDiv += "<li><a href=\"#\" ondblclick=\"planDeleteAjax('" + data[i].planNo + "'); \" >" + (i + 1) + ". " + data[i].planNm + "</a></li>";  
				}         
			}       
			$("#planMainDiv").html(planMainDiv);   
			$("#planMainDiv").listview('refresh');   
		}
	});
}

function planRegistAjax() {
	if($("#planNm").val() == "" || $("#planNm").val() == null){
		alert("입력해주세요.");
		return;
	}
	$.ajax({
		type:"post"
		,url:"/plan/planRegistAjax.do"
		,data:{"planNm":$("#planNm").val()
			  ,"planGrpNo":$("#planGrpNo").val()  
		}  
		,dataType:"json"
		,success:function(data){   
			if(data == 1){  
				$("#planNm").val("");    
				planListAjax($("#planGrpNo").val());
			}  
		} 
	});
}      

function planGrpListAjax() { 
	$("#planGrpNo").val(""); 
	$("#registBtn").attr("href", "javascript:planGrpRegistAjax();");
	$.ajax({   
		type:"post"
		,url:"/plan/planGrpListAjax.do"
		,data:{"":""}     
		,dataType:"json"     
		,success:function(data){
			var planGrpMainDiv = "";     
			if(data.length > 0){    
				for(var i=0; i<data.length;i++){             
					planGrpMainDiv += "<li><a onclick=\"setPlanGrpNo("+ data[i].planGrpNo +")\" href=\"#\" data-rel=\"popup\" data-role=\"button\" data-inline=\"true\" data-transition=\"slideup\" data-icon=\"gear\" data-theme=\"e\"); \" ><span class=\"ui-li-count\">" + data[i].planCnt + "</span>" + (i + 1) + ". " + data[i].planNm + "</a></li>";  
				}                   
			}             
			$("#planMainDiv").html(planGrpMainDiv);     
			$("#planMainDiv").listview('refresh');       
		}     
	});         
}  

function planGrpRegistAjax() {
	if($("#planNm").val() == "" || $("#planNm").val() == null){
		alert("입력해주세요.");
		return;
	}
	
	$.ajax({
		type:"post" 
		,url:"/plan/planGrpRegistAjax.do"
		,data:{"planNm":$("#planNm").val()}  
		,dataType:"json"  
		,success:function(data){   
			if(data == 1){  
				$("#planNm").val("");
				planGrpListAjax();
			}
		} 
	});
}     

function planGrpDeleteAjax(planGrpNo){
	$('#popupMenu').popup('close');
	$.ajax({ 
		type:"post"     
		,url:"/plan/planGrpDeleteAjax.do"  
		,data:{"planGrpNo":planGrpNo}
		,dataType:"json"
		,success:function(data){
			if(data == 1){  
				planGrpListAjax();   
			}
		}
	});
} 
 
function planGrpUpdateForm(planGrpNo) {
    $("#grpUpdateProcBtn").attr("href", "javascript:planGrpUpdateAjax('" +planGrpNo + "');");
    $("#planGrpUpdateForm").show();      
}     

function planGrpUpdateAjax(planGrpNo){

if($("#planUpdateNm").val() == "" || $("#planUpdateNm").val() == null){
	alert("입력해주세요.");
	return;
}    
	$.ajax({    
		type:"post"   
		,url:"/plan/planGrpUpdateAjax.do"   
		,data:{"planGrpNo":planGrpNo, "planNm":$("#planUpdateNm").val()}
		,dataType:"json"    
		,success:function(data){
			if(data == 1){
				$('#popupMenu').popup('close');
			    $("#planGrpUpdateForm").hide();
				planGrpListAjax();
			}
		}
	});
}

function setPlanGrpNo(planGrpNo){
    $("#grpMoveBtn").attr("href", "javascript:planListAjax('" +planGrpNo + "');");     
    $("#grpUpdateBtn").attr("href", "javascript:planGrpUpdateForm('" +planGrpNo + "');");
    $("#grpDeleteBtn").attr("href", "javascript:planGrpDeleteAjax('" +planGrpNo + "');");   
	$('#popupMenu').popup('open');
	$("planGrpNo").val(planGrpNo);
}
</script>
<div  data-role="content">
<input type="hidden" name="planGrpNo" id="planGrpNo">  
<ul id="planMainDiv"id="planMainDiv" data-role="listview" data-count-theme="c" data-inset="true">
</ul>  
<form>    
	<input type="text" name="planNm" id="planNm" class="form-control" placeholder="내용을 입려하세요">    
	<a id="registBtn" href="javascript:planGrpRegistAjax();" data-role="button" data-inline="true">저장</a>
	<a href="javascript:planGrpListAjax();" data-role="button" data-inline="true">뒤로가기</a>  
</form>
    
<div data-role="popup" id="popupMenu" data-theme="d">
        <ul data-role="listview" data-inset="true" style="min-width:210px;" data-theme="d">
            <li data-role="divider" data-theme="e">Choose an action</li>
            <li><a href="#" id="grpMoveBtn">이동</a></li>
            <li><a href="#" id="grpUpdateBtn">수정</a></li>   
            <li><a href="#" id="grpDeleteBtn">삭제</a></li>
        </ul>
</div>  
</div>  